package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;








import com.oocl.util.DBUtil;
import com.oocl.dao.MerchantDao;
import com.oocl.pojo.Merchant;

public class MerchantDaoImpl implements MerchantDao {

	@Override
	public int addMerchant(Merchant m) {
		String sql="insert into Merchant(mid,mname,mpwd,midcardimg,mheadimg,maddr,mstatus)values(?,?,?,?,?,?,?)";
		Connection conn=null;
		PreparedStatement pst =null;
		StringBuilder sb= new StringBuilder();
		UUID uuid = null;
		String mId = null;
		int n=0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			uuid = UUID.randomUUID();
			mId = uuid.toString();
			pst.setString(1, mId);
			pst.setString(2, m.getmName());
			pst.setString(3, m.getmPwd());
			pst.setString(4, m.getmIdCardImg());
			pst.setString(5, m.getmHeadImg());
			pst.setString(6, m.getmAddr());
			pst.setInt(7, m.getmStatus());
			n = pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(null, pst, conn);
		}
		return n;
	}

	@Override
	public Merchant findOneMerchantByMName(String mName) {
		String sql="select * from merchant where mname = ?";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		Merchant m = null;
		String favsS= null;
		int n = 0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, mName);
			rs=pst.executeQuery();
			while(rs.next()){
				m = new Merchant();
				m.setmId(rs.getString("mid"));
				m.setmName(rs.getString("mname"));
				m.setmPwd(rs.getString("mpwd"));
				m.setmIdCardImg(rs.getString("midcardimg"));
				m.setmHeadImg(rs.getString("mheadimg"));
				m.setmAddr(rs.getString("maddr"));
				m.setmStatus(rs.getInt("mstatus"));
			}
			n=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(rs, pst, conn);
		}
		return m;
	}

	@Override
	public int updateMerchant(Merchant m) {
		String sql="update merchant set mname=?,mpwd=?,midcardimg=?,mheadimg=?,maddr=?,mstatus=? where mid =?";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		StringBuilder sb= new StringBuilder();
		int n=0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, m.getmName());
			pst.setString(2, m.getmPwd());
			pst.setString(3, m.getmIdCardImg());
			pst.setString(4, m.getmHeadImg());
			pst.setString(5, m.getmAddr());
			pst.setInt(6, m.getmStatus());
			pst.setString(7, m.getmId());
			n = pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(null, pst, conn);
		}
		return n;
	}
	
//	@Override
//	public Merchant findOneMerchantByDNameAndMId(String dName, String mId) {
//		String sql="select * from merchant where mid = ? and mid in (select distinct mid from dish where dname like ?)";
//		PreparedStatement pst =null;
//		ResultSet rs = null;
//		Connection conn = null;
//		Merchant m = null;
//		String favsS= null;
//		int n = 0;
//		try {
//			conn=DBUtil.getConnection();
//			pst=conn.prepareStatement(sql);
//			pst.setString(1, mId);
//			pst.setString(2, "%"+dName+"%");
//			rs=pst.executeQuery();
//			while(rs.next()){
//				m = new Merchant();
//				m.setmId(rs.getString("mid"));
//				m.setmName(rs.getString("mname"));
//				m.setmPwd(rs.getString("mpwd"));
//				m.setmIdCardImg(rs.getString("midcardimg"));
//				m.setmHeadImg(rs.getString("mheadimg"));
//				m.setmAddr(rs.getString("maddr"));
//				m.setmStatus(rs.getInt("mstatus"));
//			}
//			n=pst.executeUpdate();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		finally{
//			DBUtil.release(rs, pst, conn);
//		}
//		return m;
//	}
//
//
//	@Override
//	public Merchant findOneMerchantByAllThree(String dName, Integer dType,
//			String mId) {
//		String sql="select * from merchant where mid = ? and mid in (select distinct mid from dish where dname like ? and dtype = ?)";
//		PreparedStatement pst =null;
//		ResultSet rs = null;
//		Connection conn = null;
//		Merchant m = null;
//		String favsS= null;
//		int n = 0;
//		try {
//			conn=DBUtil.getConnection();
//			pst=conn.prepareStatement(sql);
//			pst.setString(1, mId);
//			pst.setString(2, "%"+dName+"%");
//			pst.setInt(3, dType);
//			rs=pst.executeQuery();
//			while(rs.next()){
//				m = new Merchant();
//				m.setmId(rs.getString("mid"));
//				m.setmName(rs.getString("mname"));
//				m.setmPwd(rs.getString("mpwd"));
//				m.setmIdCardImg(rs.getString("midcardimg"));
//				m.setmHeadImg(rs.getString("mheadimg"));
//				m.setmAddr(rs.getString("maddr"));
//				m.setmStatus(rs.getInt("mstatus"));
//			}
//			n=pst.executeUpdate();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		finally{
//			DBUtil.release(rs, pst, conn);
//		}
//		return m;
//	}
	
	@Override
	public Merchant findOneMerchantByMId(String mId) {
		String sql="select * from merchant where mid = ?";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		Merchant m = null;
		String favsS= null;
		int n = 0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, mId);
			rs=pst.executeQuery();
			while(rs.next()){
				m = new Merchant();
				m.setmId(rs.getString("mid"));
				m.setmName(rs.getString("mname"));
				m.setmPwd(rs.getString("mpwd"));
				m.setmIdCardImg(rs.getString("midcardimg"));
				m.setmHeadImg(rs.getString("mheadimg"));
				m.setmAddr(rs.getString("maddr"));
				m.setmStatus(rs.getInt("mstatus"));
			}
			n=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(rs, pst, conn);
		}
		return m;
	}

	@Override
	public Merchant findOneMerchantByDTypeAndMId(Integer dType, String mId) {
		String sql="select * from merchant where mid = ? and mid in (select distinct mid from dish where dtype = ?)";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		Merchant m = null;
		String favsS= null;
		int n = 0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, mId);
			pst.setInt(2, dType);
			rs=pst.executeQuery();
			while(rs.next()){
				m = new Merchant();
				m.setmId(rs.getString("mid"));
				m.setmName(rs.getString("mname"));
				m.setmPwd(rs.getString("mpwd"));
				m.setmIdCardImg(rs.getString("midcardimg"));
				m.setmHeadImg(rs.getString("mheadimg"));
				m.setmAddr(rs.getString("maddr"));
				m.setmStatus(rs.getInt("mstatus"));
			}
			n=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(rs, pst, conn);
		}
		return m;
	}

	@Override
	public Merchant findOneMerchantByDNameAndMId(String Name, String mId) {
		String sql="select * from merchant where mid = ? and (mid in (select distinct mid from dish where dname like ?) or mid in (select distinct mid from merchant where mname like ?))";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		Merchant m = null;
		String favsS= null;
		int n = 0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, mId);
			pst.setString(2, "%"+Name+"%");
			pst.setString(3, "%"+Name+"%");
			rs=pst.executeQuery();
			while(rs.next()){
				m = new Merchant();
				m.setmId(rs.getString("mid"));
				m.setmName(rs.getString("mname"));
				m.setmPwd(rs.getString("mpwd"));
				m.setmIdCardImg(rs.getString("midcardimg"));
				m.setmHeadImg(rs.getString("mheadimg"));
				m.setmAddr(rs.getString("maddr"));
				m.setmStatus(rs.getInt("mstatus"));
			}
			n=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(rs, pst, conn);
		}
		return m;
	}

	@Override
	public Merchant findOneMerchantByAllThree(String Name, Integer dType,
			String mId) {
		String sql="select * from merchant where mid = ? and mid in (select distinct mid from dish where dtype = ?) and (mid in (select distinct mid from dish where dname like ?) OR mid in (select distinct mid from merchant where mname like ?))";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		Merchant m = null;
		String favsS= null;
		int n = 0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, mId);
			pst.setInt(2,dType);
			pst.setString(3, "%"+Name+"%");
			pst.setString(4, "%"+Name+"%");
			rs=pst.executeQuery();
			while(rs.next()){
				m = new Merchant();
				m.setmId(rs.getString("mid"));
				m.setmName(rs.getString("mname"));
				m.setmPwd(rs.getString("mpwd"));
				m.setmIdCardImg(rs.getString("midcardimg"));
				m.setmHeadImg(rs.getString("mheadimg"));
				m.setmAddr(rs.getString("maddr"));
				m.setmStatus(rs.getInt("mstatus"));
			}
			n=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(rs, pst, conn);
		}
		return m;
	}

	@Override
	public String MerchantIdFilterByStatus(String mId) {
		String sql="select mid from merchant where mid = ? and mstatus = 1";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		String mid = null;
		int n = 0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, mId);
			rs=pst.executeQuery();
			while(rs.next()){
				mid = rs.getString("mid");
			}
			n=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(rs, pst, conn);
		}
		return mid;
	}

	
	
	

}
